package com.example.asus.yuejian_10.fragment;



import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.asus.yuejian_10.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    private FragmentManager manager;
    private RadioGroup btn;
    private RadioButton btn_activity;
    private RadioButton btn_teacher;
    private RadioButton btn_game;
    private TeacherFragment teacherFragment;
    private GameFragment gameFragment;
    private ActivityFragment activityFragment;
    private View view;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.view=view;
        init();
        btn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.btn_activity:
                        replace(activityFragment);
                        break;
                    case R.id.btn_game:
                        replace(gameFragment);
                        break;
                    case R.id.btn_teacher:
                        replace(teacherFragment);
                        break;
                }
            }
        });


    }

    private void init() {
        manager=getChildFragmentManager();
        btn=(RadioGroup)view.findViewById(R.id.btn);
        btn_activity=(RadioButton)view.findViewById(R.id.btn_activity);
        btn_teacher=(RadioButton)view.findViewById(R.id.btn_teacher);
        btn_game=(RadioButton)view.findViewById(R.id.btn_game);
        activityFragment=new ActivityFragment();
        teacherFragment=new TeacherFragment();
        gameFragment=new GameFragment();
        FragmentManager fragmentManager=getChildFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.frag_lay,new ActivityFragment());
        transaction.commit();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    public void replace(Fragment fragment)
    {
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.frag_lay,fragment);
        transaction.commit();
    }
}
